#include <asm.h>

.section ".entry_function","ax"
ENTRY(_start)

    /* TODO: [p1-task3] setup C runtime environment for the user program */
    la  a1, __bss_start
    la  a2, __BSS_END__
    bgt a1, a2, Bss_Clear_Done

Bss_Clear:
    sb   zero, 0(a1)
    addi a1, a1, 1
    ble  a1, a2, Bss_Clear
Bss_Clear_Done:
    

    /* TODO: [p1-task3] enter main function */
    addi sp, sp, -16
    sd   fp, 8(sp)
    sd   ra, 0(sp)
    call main
    ld   ra, 0(sp)
    ld   fp, 8(sp)
    addi sp, sp, 16

    /* TODO: [p1-task3] finish task and return to the kernel */
    jr ra
    /* NOTE: You need to replace this with new mechanism in p3-task2! */


// while(1) loop, unreachable here
loop:
    wfi
    j loop

END(_start)
